#!/usr/bin/env python3
# coding: utf-8
"""
2025-05-30 检查项目
"""

from PySide6.QtCore import Slot, QSize
from PySide6.QtGui import QAction, QKeySequence,QIntValidator, QTextCursor   
from PySide6.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QWidget, QMessageBox, QLineEdit, QTableWidget, QTableWidgetItem, QHeaderView, QAbstractItemView, QLabel,QCheckBox,QTextEdit,QGroupBox
from PySide6.QtCore import Qt, QModelIndex,QMetaObject,Q_ARG
from PySide6.QtGui import QGuiApplication, QCloseEvent
## from PySide6.QtCore.Qt import 


##from PySide6.QtCore import WindowState

## from top.wardyan.core.utils import winformutils
from top.wardyan.core.action.baseaction import WMainWindow,WQdialog
from top.wardyan.core.vo.vos import *
from top.wardyan.core.widges.tables import WTableView

##from warningmain import *
from top.wardyan.core.wdb.core.DBPool import POOL

from top.wardyan.core.vo.vos import *
from top.wardyan.core.wdb.core.db import *
from config import Config
from top.wardyan.core.sqlite import sqliteconfigs

import threading
import time
import re
import sys

from apscheduler.schedulers.background import BackgroundScheduler
from top.wardyan.core.utils.common import *
from top.wardyan.projects.checktool_manager.main.checktool_manager_yt_check_edit import *

import uuid

from top.wardyan.projects.checktool_manager.main.checktool_manager_const import *


SQL_LISTALL = "EXEC dbo.yp_check_view @pBeginDate = '1900-01-01', @pEndDate = '2078-12-31 23:59:59', @pText = %s, @pKind = 0, @pType = 0,@pProjectID=%d"



        

class ChecktoolCheckShell (WQdialog) :
        
    def __init__(self) -> None:
        ##global cn
        super().__init__("ChecktoolCheckShell")
        self.table : WTableView = None
        self.main()
        self.refreshTable()


    def button_click_search(self) :
        self.refreshTable()


    def button_click_add(self) :
        win = ChecktoolCheckEditShell(0)
        rs : int = win.exec()
        self.refreshTable()


    # def handle_double_click(self) :
    #     ## 获得表格 中选中的行索引

    #     # shell = ChecktoolCheckEditShell()
    #     # shell.show()
    #     # 获取选中行的索引
    #     selected_indexes = self.table.selectionModel().selectedIndexes()

    #     if selected_indexes:
    #         # 取得选中行第一列的值

    #         row = selected_indexes[0].row()
    #         print(f"双击的行索引为: {row}")
    #         first_column_index = self.table.model().index(row, 0)  # 第一行第0列的数据
    #         # 获取该单元格的数据
    #         first_column_value = self.table.model().data(first_column_index)
    #         print(f"选中行第一列的值为: {first_column_value}")

    #         # 传递当前行数据给编辑窗口
    #         shell = ChecktoolCheckEditShell()
    #         ##shell.load_data(row)  # 假设有一个 load_data 方法用于加载数据
    #         shell.show()
    #     else:
    #         print("未选中任何行")


    
    def handle_double_click(self, index: QModelIndex) -> None:
        """
        处理单元格的双击事件。
        :param index: 被双击的单元格的模型索引。
        """
        row = index.row()
        column = index.column()
        ##print(f"Double click detected at Row: {row}, Column: {column}")
        data : Record = self.table.get_row_data(row)
        if data is not None :
            field = data.get("sID")
            ## win = TableColEditor("tablemanagercoledit", self.tablename, field)
            win = ChecktoolCheckEditShell(field)
            # win.exec()
            rs : int = win.exec()
            # if rs is not None and rs > 0 :
            self.refreshTable()


    def main(self) :

        HEIGHT = 40
        layout : QVBoxLayout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignVCenter)
        self.setLayout(layout)
        
        

        ## 按钮: 搜索 新增
        topwidget : QWidget = QWidget()
        ##topwidget.setFixedHeight(100)
        layout.addWidget(topwidget)
        topwidget.setFixedHeight(50)
        
        # 设置背景颜色
        #topwidget.setStyleSheet("background-color: #ffff00;padding:0;margin:0;")  # 你可以将 #f0f0f0 替换为你想要的颜色代码

        toplayout : QHBoxLayout = QHBoxLayout()
        toplayout.setAlignment(Qt.AlignmentFlag.AlignRight)
        ##self.setLayout(layout)
        ##layout.addWidget(toplayout)
        topwidget.setLayout(toplayout)
        

        self.rProjectIDComboBox = QComboBox()
        self.rProjectIDComboBox.setFixedHeight(HEIGHT)
        self.rProjectIDComboBox.addItem("全部", 0)
        self.rProjectIDComboBox.addItem("数据库检查工具", 7)
        self.rProjectIDComboBox.addItem("数据分析工具", 10)
        self.rProjectIDComboBox.addItem("定时任务", 16)
            #self.xKindComboBox.setEditText("不限")
        self.rProjectIDComboBox.setFixedWidth(200)
        default_index = self.rProjectIDComboBox.findText("全部")  # 假设你想默认选中 "类型A"
        if default_index >= 0:
            self.rProjectIDComboBox.setCurrentIndex(default_index)
        toplayout.addWidget(self.rProjectIDComboBox)


        self.txtKey : QLineEdit = QLineEdit()
        self.txtKey.setFixedHeight(HEIGHT)
        self.txtKey.setFixedWidth(200)
        self.txtKey.setText("")
        toplayout.addWidget(self.txtKey)
        ##self.txtKey.textChanged.connect(self.text_max_idle_change)
        ## txtDesc只允许输入数字
        ##self.txtKey.setValidator(QIntValidator())
        # 连接回车键事件到 button_click_search 方法
        self.txtKey.returnPressed.connect(self.button_click_search)



        ### 
        

        btnSearch : QPushButton = QPushButton("搜索")
        btnSearch.setFixedHeight(HEIGHT)
        btnSearch.setFixedWidth(150)
        btnSearch.clicked.connect(self.button_click_search)
        btnSearch.setDefault(True)
        toplayout.addWidget(btnSearch)



        btnAdd : QPushButton = QPushButton("新增")
        btnAdd.setFixedHeight(HEIGHT)
        btnAdd.setFixedWidth(150)
        btnAdd.clicked.connect(self.button_click_add)
        toplayout.addWidget(btnAdd)

        ###################

        ctrlw : QWidget = QWidget()
        ctrllayout : QHBoxLayout = QHBoxLayout()
        ctrlw.setLayout(ctrllayout)
        layout.addWidget(ctrlw)


        ctrllayout.setAlignment(Qt.AlignmentFlag.AlignRight)
        

        ctrlt : QWidget = QWidget()
        tbllayout : QHBoxLayout = QHBoxLayout()
        ctrlt.setLayout(tbllayout)
        layout.addWidget(ctrlt)

        self.table = WTableView(self, "checktool_manager_check", "maintable", dbclick = self.handle_double_click)
        tbllayout.addWidget(self.table)

    @Slot()
    def refreshTable(self) :

        data : list[Record] = []
        # 查看正在执行的语句
        key : str = self.txtKey.text()
        if key is None :
            key = ""
        if key == " " :
            key = ""
        if key == "" :
            key = None
        projectID = self.rProjectIDComboBox.currentData()
        ## 暂时不限制日期范围等条件
        rs = query(sql=SQL_LISTALL, params= (key,projectID),config=MYCONFIG)
        if rs is None :
            rs = []
        data = listtorecords(rs)
        ## print(rs)
        self.data = data
        self.table.render(data)
        
        

# if __name__ == "__main__" :     ## or 1 == 1
#     app = QApplication([])
#     w = WWarningEditor()
#     ## w = WScriptWindow()
#     w.show()
#     app.exec()